#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Version: python2.7
Author: Qiao Da Zhi
Module Description: hunt ... 
File: hunt.py
Date: 2017/9/12
"""
from pyshare.utils.time_utils import isPassDate, getCD


class HuntMixin(object):
    __slots__ = ()
    role_guid = NotImplemented
    hunt_total_score = NotImplemented
    can_use_score = NotImplemented
    item_exchange_time_by_score = NotImplemented
    item_exchange_time_by_item = NotImplemented
    refresh_date = NotImplemented
    minister_noon_hunt_list = NotImplemented
    minister_noon_recover_list = NotImplemented
    minister_night_hunt_list = NotImplemented
    minister_night_recover_list = NotImplemented
    accident_event_time = NotImplemented
    current_scene = NotImplemented
    current_grass = NotImplemented
    current_monster = NotImplemented
    current_monster_hp = NotImplemented
    killed_monster_num = NotImplemented
    award_status = NotImplemented
    is_reward = NotImplemented
    has_attack_boss = NotImplemented
    total_hunt_damage = NotImplemented
    damage_update_date = NotImplemented

    def simple_dict(self, key_list, load=True):
        """以args为key精简数据"""
        data = self.asdict()
        simple_data = {}
        try:
            if load:
                for key in key_list:
                    simple_data[key] = data[key]
            else:
                for key in data:
                    if key not in key_list:
                        simple_data[key] = data[key]

        except Exception as e:
            simple_data = data

        return simple_data

    def reset_daily_data(self, save_immediately=True):
        """重置每日数据"""
        if isPassDate(self.refresh_date):
            self.refresh_date = getCD()
            self.item_exchange_time_by_score = {}
            self.item_exchange_time_by_item = {}
            self.minister_noon_hunt_list = []
            self.minister_noon_recover_list = []
            self.minister_night_hunt_list = []
            self.minister_night_recover_list = []
            self.accident_event_time = 0
            self.current_scene = 0
            self.current_grass = []
            self.current_monster = 0
            self.current_monster_hp = 0
            self.killed_monster_num = 0
            self.award_status = 1
            self.is_reward = 0
            self.has_attack_boss = 0
            self.total_hunt_damage = 0
            if save_immediately:
                self.save()
            return True
        return False

    def reduce_prey_hp(self, fight_power, minister_id):
        """减少猎物血量"""
        # 获取猎物受到的伤害
        damage = min(fight_power, self.current_monster_hp)
        self.current_monster_hp -= damage
        if self.current_monster_hp == 0:
            # 更新击杀进度
            self.killed_monster_num += 1

        # 更新大臣到已狩猎列表
        noon_hunt_list_cache = self.minister_noon_hunt_list
        noon_hunt_list_cache.append(minister_id)
        self.minister_noon_hunt_list = noon_hunt_list_cache

        return damage

    def add_hunt_score(self, add_value, log_param):
        """增加围猎积分"""
        # 增加围猎积分
        self.hunt_total_score += add_value
        self.can_use_score += add_value

        # 更新围猎积分排行榜
        from common.service_config import get_server_id, get_app_id
        from pyshare.bll.utils.area.rank_utils import get_rank_manager
        from pyshare.const.enums.leaderboard import hunt_score_rank
        rank_key = hunt_score_rank.format(srv_id=get_server_id(), app_id=get_app_id())
        get_rank_manager().add_to_rank(self.role_guid, self.hunt_total_score, rank_key)

        # 围猎积分日志
        from pyshare.utils.game_log.game_log_define import HuntScoreLog
        HuntScoreLog.log_game(log_param=log_param,
                              hunt_score=self.can_use_score,
                              add_value=add_value)

    def add_hunt_boss_damage(self, add_value, minister_id):
        """增加今日对boss的伤害"""
        # 增加伤害
        if isPassDate(self.damage_update_date):
            self.damage_update_date = getCD()
            self.total_hunt_damage = 0
        self.total_hunt_damage += add_value

        # 更新大臣到已狩猎列表
        night_hunt_list_cache = self.minister_night_hunt_list
        night_hunt_list_cache.append(minister_id)
        self.minister_night_hunt_list = night_hunt_list_cache

        # 更新boss伤害排行榜
        from common.service_config import get_server_id, get_app_id
        from pyshare.bll.utils.area.rank_utils import get_rank_manager
        from pyshare.const.enums.leaderboard import hunt_boss_damage_rank
        rank_key = hunt_boss_damage_rank.format(srv_id=get_server_id(), app_id=get_app_id())
        get_rank_manager().add_to_rank(self.role_guid, self.total_hunt_damage, rank_key)

